﻿using System.Web.Mvc;
using System.Linq;
using System.Data.Entity;
using HelloWorld.Code.DataAccess;
using HelloWorld.Code.Security;

namespace HelloWorld.Controllers
{
    [Authorize]
    public class UserControlsController : Controller
    {
        public ActionResult PartialViewExample()
        {
            var userId = HttpContext.User.ToCustomPrincipal().CustomIdentity.UserId;
            using (var context = new MvcDemoEntities())
            {
                var user = context.Users.Include(u => u.ShippingAddress).Include(u => u.BillingAddress).First(u => u.UserId == userId);
                return View(user);
            }
        }

        [HttpGet]
        public ActionResult EditLoggedUserAddress(string type)
        {
            var userId = HttpContext.User.ToCustomPrincipal().CustomIdentity.UserId;
            using (var context = new MvcDemoEntities())
            {
                var user = context.Users.Include(u => u.ShippingAddress).Include(u => u.BillingAddress).First(u => u.UserId == userId);
                var address = string.Compare(type, "ShippingAddress", true) == 0 ? user.ShippingAddress : user.BillingAddress;
                if (address == null)
                    address = new Address();

                address.IsReadOnly = false;
                return PartialView("~/Views/PartialViews/AddressPartialView.cshtml", address);
            }
        }

        [HttpPost]
        public ActionResult SaveLoggedUserAddress(Address addressModel, string type)
        {
            if (!ModelState.IsValid)
            {
                addressModel.IsReadOnly = false;
                return PartialView("~/Views/PartialViews/AddressPartialView.cshtml", addressModel);
            }

            var userId = HttpContext.User.ToCustomPrincipal().CustomIdentity.UserId;
            var isShippingAddress = System.String.Compare(type, "ShippingAddress", System.StringComparison.OrdinalIgnoreCase) == 0;
            using (var context = new MvcDemoEntities())
            {
                var user = context.Users.Include(u => u.ShippingAddress).Include(u => u.BillingAddress).First(u => u.UserId == userId);
                var address = isShippingAddress ? user.ShippingAddress : user.BillingAddress;
                if (address == null)
                {
                    address = addressModel;
                    context.AddToAddresses(address);
                    if (isShippingAddress)
                        user.ShippingAddress = address;
                    else
                        user.BillingAddress = address;
                }

                address.Street = addressModel.Street;
                address.ZipCode = addressModel.ZipCode;
                address.City = addressModel.City;
                address.Country = addressModel.Country;
                context.SaveChanges();

                address.IsReadOnly = true;
                return PartialView("~/Views/PartialViews/AddressPartialView.cshtml", address);
            }
        }
    }
}